home *** CD-ROM | disk | FTP | other *** search
- From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
- Message-ID: <4j6tgs$rhm@news.BelWue.DE>
- X-Original-Date: 25 Mar 1996 19:51:56 GMT
- Path: in2.uu.net!bounce-back
- Date: 26 Mar 96 04:46:07 GMT
- Approved: fjh@cs.mu.oz.au
- Newsgroups: comp.std.c++
- Subject: Re: constness of private members and methods
- Organization: Fakultdt f|r Mathematik und Informatik
- References: <4j49e0$8fo@dub-news-svc-4.compuserve.com>
- Reply-To: dietmar.kuehl@uni-konstanz.de
- X-Newsreader: TIN [version 1.2 PL2]
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMVd2p+EDnX0m9pzZAQHqCAF/ZDO6HPcBVdic8NAEUIGCnsaWOyQKu1+G
- N2prj3MkxNv3ceH1NeLoQJz61IT3Mo+k
- =KXII
-
- Hi,
-
- Philippe Verdy (100105.3120@compuserve.com) wrote:
- [lengthy description of problems with bitwise instead of logical
- constness elided]
-
- : Is there any way (or proposal) to explicitly specify members
- : of a class definition which are relaxed on constness certi-
- : fication ?
-
- Yes: it is possible to declare a data member of an object to be
- 'mutable'. This allows to modify this member even in a 'const'
- function. The main idea which leads to a reasonable use of 'mutable' is
- the concept of logical versus bitwise constness: In C++ bitwise
- constness is generally assumed. You cannot change the member of a
- constant object. However, sometimes this is not what you want (auxilary
- marks and cached values are two examples). Instead, logical constness
- is desired: The representation of the object might change but the value
- represented stays the same. Note, however, that the language does not
- (and can not) enforce that logical constness is maintained if the
- object has mutable members. It is the responsibility of the programmer
- to ensure logical constness.
-
- : Would not it be great if we could simply define such members
- : as "unconst" like in the following example :
-
- : class T {
- : public:
- : T() { ...; _mark = 0; }
- : virtual ~T();
-
- : virtual void show() const;
- : protected:
- : void mark() const { _mark = 1; } // RELAXED HERE
- : void unmark() const { _mark = 0; } // RELAXED HERE
- : bool ismarked() const { return _mark; }
- : private:
- : unconst char _mark; // HERE IS WHERE THIS APPLIES
- : }
-
- If you replace 'unconst' by 'mutable' you have what will become
- standard. Since the 'mutable' keyword is a recent addition to the
- DWP, it is not yet supported by all compilers.
- --
- dietmar.kuehl@uni-konstanz.de
- http://www.informatik.uni-konstanz.de/~kuehl
- I am a realistic optimist - that's why I appear to be slightly pessimistic
- ---
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-